/*global pi */

pi.mod.define('pi.render.camera', function (pi) {
	"use strict";
	var makeViewMat, updateViewMat, module = {},
		tmpV = pi.math.vec3.create(0, 0, 0),
		tmpQ = pi.math.quat.create(1, 0, 0, 0);

	makeViewMat = function (dst, posVec, qOrient) {
		// View matrix is:
		//
		//  [ Lx  Uy  Dz  Tx  ]
		//  [ Lx  Uy  Dz  Ty  ]
		//  [ Lx  Uy  Dz  Tz  ]
		//  [ 0   0   0   1   ]
		// Where T = -(Transposed(Rot) * Pos)
		pi.math.mat4.rotate(dst, qOrient);
		pi.math.mat4.transpose(dst, dst);
		pi.math.vec3.negative(tmpV, posVec);
		pi.math.mat4.applyVec(tmpV, dst, tmpV);

		dst[3] = tmpV.x;
		dst[7] = tmpV.y;
		dst[11] = tmpV.z;
	};

	updateViewMat = function (camera) {
		if (!camera.viewMat) {
			camera.viewMat = pi.math.mat4.create();
		}
		makeViewMat(camera.viewMat, camera.position, camera.orientation);
	};

	module.create = function () {
		var r = {
			// y轴是否固定
			isYawFixed: true,
			// 固定的y轴
			yawFixAxis: pi.math.vec3.create(0, 1, 0),
			// 位置
			position: pi.math.vec3.create(),
			// 方位
			orientation: pi.math.quat.create(1, 0, 0, 0),
			// 视图矩阵
			viewMat: pi.math.mat4.create(),
			// 投影矩阵
			projMat: pi.math.mat4.create(),
			// 宽高比
			ratio: 0.0,
			// zy面角度，单位：弧度
			fovRad: 0.0,
			// 近裁剪面距离
			disNear: 1.0,
			// 近裁剪面距离
			disFar: 1000.0
		};

		pi.math.mat4.copy(r.viewMat, pi.math.mat4.IDENTITY);
		pi.math.mat4.copy(r.projMat, pi.math.mat4.IDENTITY);
		return r;
	};

	// 初始化相机视图
	module.initView = function (camera, posVec, quat) {
		camera.isYawFixed = true;
		if (!camera.yawFixAxis) {
			camera.yawFixAxis = pi.math.vec3.create();
		}
		pi.math.vec3.copy(camera.yawFixAxis, pi.math.vec3.Y_UNIT);

		if (!camera.projMat) {
			camera.projMat = pi.math.mat4.create();
		}
		pi.math.mat4.copy(camera.projMat, pi.math.mat4.IDENTITY);

		if (!camera.position) {
			camera.position = pi.math.vec3.create();
		}
		pi.math.vec3.copy(camera.position, posVec);
		if (!quat) {
			quat = pi.math.quat.UNIT;
		}
		if (!camera.orientation) {
			pi.math.quat.copy(camera.orientation, quat);
		}
		updateViewMat(camera);
	};

	// 初始化相机投影
	module.initProj = function (camera, fovRad, ratio, disNear, disFar) {
		camera.ratio = ratio;
		camera.fovRad = fovRad;
		camera.disFar = disFar;
		camera.disNear = disNear;
		if (!camera.projMat) {
			camera.projMat = pi.math.mat4.create();
		}
		pi.math.mat4.perspective(camera.projMat, fovRad, ratio, disNear, disFar);
	};

	// 设置相机的固定轴
	module.setYawFixed = function (camera, isFixed, fixedAxis) {
		camera.isYawFixed = isFixed;
		pi.math.vec3.copy(camera.yawFixAxis, fixedAxis);
	};

	// 取得相机的方向
	module.getDirection = function (camera, dst) {
		pi.math.vec3.negative(tmpV, pi.math.vec3.Z_UNIT);
		pi.math.quat.rotateVec(dst, camera.orientation, tmpV);
	};

	// 得到相机的右向量
	module.getRight = function (camera, dst) {
		pi.math.quat.rotateVec(dst, camera.orientation, pi.math.vec3.X_UNIT);
	};

	// 得到相机的上向量
	module.getUp = function (camera, dst) {
		pi.math.quat.rotateVec(dst, camera.orientation, pi.math.vec3.Y_UNIT);
	};

	// 移动向量moveVec，世界坐标
	module.move = function (camera, moveVec) {
		pi.math.vec3.add(camera.position, camera.position, moveVec);
		updateViewMat(camera);
	};

	// 相机沿着任意轴旋转
	module.rotate = function (camera, axis, radAng) {
		pi.math.quat.fromAxisAngle(tmpQ, axis, radAng);
		pi.math.quat.normalize(tmpQ, tmpQ);
		pi.math.quat.mul(camera.orientation, tmpQ, camera.orientation);
		updateViewMat(camera);
	};

	// 沿着自身y轴旋转
	module.yaw = function (camera, radAng) {
		var yAxis, yUnit = pi.math.vec3.Y_UNIT;
		if (camera.isYawFixed) {
			yAxis = camera.yawFixAxis;
		} else {
			pi.math.quat.rotateVec(tmpV, camera.orientation, yUnit);
			yAxis = tmpV;
		}
		module.rotate(camera, yAxis, radAng);
	};

	// 沿着自身的x轴转
	module.pitch = function (camera, radAng) {
		var xUnit = pi.math.vec3.X_UNIT;
		pi.math.quat.rotateVec(tmpV, camera.orientation, xUnit);
		module.rotate(camera, tmpV, radAng);
	};

	return module;
});